package com.czy.seed.mvc.util;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

public class NotePort {
    public static Logger logger = LoggerFactory.getLogger(NotePort.class);
    @Autowired
    SystemProperties systemProperties;
    /**
     * 短信接口，勿动！
     * @author Administrator
     * 2017年2月28日
     */
        // 使用StringBuffer的append获得xml形式的字符串
        StringBuffer sub = new StringBuffer();
        BufferedReader br = null;
        URL url = null;
        HttpURLConnection con;
        String line;
       /* *//**
         * TODO :发送短信
         * @param userid    企业ID，为空
         * @param account   登陆名称
         * @param password  接口密码，不是登陆密码
         * @param mobile    发送人姓名
         * @param content   发送内容
         * @param sendTime  发送时间，为空
         * 2017年3月1日
         * Administrator : xiaogaoxiang
         */
        @SuppressWarnings("finally")
        public StringBuffer SendMessage(String userid, String account,
                                        String password, String mobile, String content, String sendTime ) {
            try {
                // 设置发送内容的编码方式
                String send_content = URLEncoder.encode(
                        content.replaceAll("<br/>", " "), "UTF-8");// 发送内容

                url = new URL("http://sz.ipyy.net/sms.aspx?action=send&userid="+ userid + "&account=" + account + "&password=" + password+ "&mobile=" + mobile + "&content=" + send_content+ "&sendTime=" + sendTime + "");
                con = (HttpURLConnection) url.openConnection();
                br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));

                while ((line = br.readLine()) != null) {
                    // 追加字符串获得XML形式的字符串
                    sub.append(line + "");
                }
                br.close();
                logger.info("发送成功！");
            } catch (IOException e) {
                logger.error("发送失败！");
            } finally {
                return sub;
            }
        }
    // XML字符串解析通用方法
    public xmlEntity readStringXmlCommen(xmlEntity xmlentity, String xml) {
        xmlEntity xe = new xmlEntity();
        Document doc = null;
        try {
            // 将字符转化为XML
            doc = DocumentHelper.parseText(xml);
            // 获取根节点
            Element rootElt = doc.getRootElement();
            // 拿到根节点的名称
            // System.out.println("根节点：" + rootElt.getName());

            // 获取根节点下的子节点的值
            if (xmlentity.getReturnstatus() != null) {
                xe.setReturnstatus(rootElt.elementText(
                        xmlentity.getReturnstatus()).trim());
            }
            if (xmlentity.getMessage() != null) {
                xe.setMessage(rootElt.elementText(xmlentity.getMessage())
                        .trim());
            }
            if (xmlentity.getRemainpoint() != null) {
                xe.setRemainpoint(rootElt.elementText(
                        xmlentity.getRemainpoint()).trim());
            }
            if (xmlentity.getTaskID() != null) {
                xe.setTaskID(rootElt.elementText(xmlentity.getTaskID()).trim());
            }
            if (xmlentity.getSuccessCounts() != null) {
                xe.setSuccessCounts(rootElt.elementText(
                        xmlentity.getSuccessCounts()).trim());
            }
            if (xmlentity.getPayinfo() != null) {
                xe.setPayinfo(rootElt.elementText(xmlentity.getPayinfo())
                        .trim());
            }
            if (xmlentity.getOverage() != null) {
                xe.setOverage(rootElt.elementText(xmlentity.getOverage())
                        .trim());
            }
            if (xmlentity.getSendTotal() != null) {
                xe.setSendTotal(rootElt.elementText(xmlentity.getSendTotal())
                        .trim());
            }
            // 接收状态返回的报告
            if (rootElt.hasMixedContent() == false) {
                System.out.println("无返回状态！");
            } else {
                for (int i = 1; i <= rootElt.elements().size(); i++) {
                    if (xmlentity.getStatusbox() != null) {
                        System.out.println("状态" + i + ":");
                        // 获取根节点下的子节点statusbox
                        Iterator<?> iter = rootElt.elementIterator(xmlentity
                                .getStatusbox());
                        // 遍历statusbox节点
                        while (iter.hasNext()) {
                            Element recordEle = (Element) iter.next();
                            xe.setMobile(recordEle.elementText("mobile").trim());
                            xe.setTaskid(recordEle.elementText("taskid").trim());
                            xe.setStatus(recordEle.elementText("status").trim());
                            xe.setReceivetime(recordEle.elementText(
                                    "receivetime").trim());
                            System.out.println("对应手机号：" + xe.getMobile());
                            System.out.println("同一批任务ID：" + xe.getTaskid());
                            System.out.println("状态报告----10：发送成功，20：发送失败："
                                    + xe.getStatus());
                            System.out.println("接收时间：" + xe.getReceivetime());
                        }
                    }

                }
            }
            // 错误返回的报告
            if (xmlentity.getErrorstatus() != null) {
                // 获取根节点下的子节点errorstatus
                Iterator<?> itererr = rootElt.elementIterator(xmlentity
                        .getErrorstatus());
                // 遍历errorstatus节点
                while (itererr.hasNext()) {
                    Element recordElerr = (Element) itererr.next();
                    xe.setError(recordElerr.elementText("error").trim());
                    xe.setRemark(recordElerr.elementText("remark").trim());
                    System.out.println("错误代码：" + xe.getError());
                    System.out.println("错误描述：" + xe.getRemark());
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return xe;
    }

}

